﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.ComponentModel;
using System.Globalization;

namespace eveDBToy1
{
    class eveAPIClientSettings
    {

    }

    public sealed class eveApiKeyConverter : ConfigurationConverterBase
    {
        public eveApiKeyConverter() { }

        /// <summary>
        /// converts a string into a eveApiKey; the format is "N: name I: id K: key"
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="ci"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public override object ConvertFrom(ITypeDescriptorContext ctx, CultureInfo ci, object data)
        {
            string dataStr = ((string)data).ToLower();
            string[] values = dataStr.Split(',');
            if (values.Length == 3)
            {
                try
                {
                    eveApiKey apiKey = new eveApiKey();
                    foreach (string value in values)
                    {
                        string[] varVal = value.Split(':');
                        switch (varVal[0])
                        {
                            case "n":
                                apiKey.Name = varVal[1];
                                break;
                            case "i":
                                apiKey.Id = Convert.ToInt32(varVal[1]);
                                break;
                            case "k":
                                apiKey.Key = varVal[1];
                                break;
                        }
                    }
                    return apiKey;
                }
                catch
                {
                    throw new ArgumentException("eveApiKeyConverter: The string contains invalid data");
                }
            }
            throw new ArgumentException("eveApiConverter: the string has an invalid number of parts");
        }

        public override object ConvertTo(ITypeDescriptorContext ctx, CultureInfo ci, object value, Type type)
        {
            return value.ToString();
        }
    }

    struct eveApiKey
    {
        string m_Name;
        int m_Id;
        string m_Key;

        //public eveApiKey() { m_Name = ""; m_Id = -1; m_Key = ""; }

        public eveApiKey(string Name, int Id, string Key)
        {
            m_Name = Name;
            m_Id = Id;
            m_Key = Key;
        }

        public string Name
        {
            get
            {
                return m_Name;
            }
            set 
            {
                m_Name = value;
            }
        }

        public int Id
        {
            get
            {
                return m_Id;
            }
            set
            {
                m_Id = value;
            }
        }

        public string Key
        {
            get
            {
                return m_Key;
            }
            set
            {
                m_Key = value;
            }
        }

        public override string ToString()
        {
            return "N: " + m_Name + "I: " + m_Id + "K: " + m_Key;
        }

    }

}
